/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import edu.gatech.jiggawatt.office.model.Patient;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

/**
 * Represents "new Patient Frame"
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class newPatientFrame extends JFrame { // $codepro.audit.disable classNamingConvention, declareDefaultConstructors

	private JPanel contentPane;
	private JTextField idField;
	private JTextField pwField;
	private JTextField fNameField;
	private JTextField lNameField;
	private JTextField ageField;
	private JTextField phoneField;
	private JTextField emailField;
	private JTextArea addressArea;
	private JRadioButton rdbtnMale;
	private JRadioButton rdbtnFemale;
	private Presenter presenter;
	private DefaultListModel model;
	private JLabel lblPharmacy;
	private JLabel lblInsurance;
	private JLabel lblAllergy;
	private JTextField pharmacyField;
	private JTextField insuranceField;
	private JScrollPane scrollPane;
	private JTextArea allergyArea;

	/**
	 * Create the frame.
	 * @param p presenter
	 * @param m patientlist model to update
	 */
	public newPatientFrame(Presenter p, DefaultListModel m) {
		presenter = p;
		model = m;
		
		setTitle("New Patient"); // $codepro.audit.disable stringLiterals
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 290, 544); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblId = new JLabel("ID:"); // $codepro.audit.disable stringLiterals
		lblId.setBounds(25, 23, 46, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblId);
		
		idField = new JTextField();
		idField.setBounds(110, 20, 114, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(idField);
		idField.setColumns(10); // $codepro.audit.disable numericLiterals
		
		JLabel lblPw = new JLabel("PW:"); // $codepro.audit.disable stringLiterals
		lblPw.setBounds(25, 53, 46, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblPw);
		
		JLabel lblFirstName = new JLabel("First Name:"); // $codepro.audit.disable stringLiterals
		lblFirstName.setBounds(25, 81, 65, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name:"); // $codepro.audit.disable stringLiterals
		lblLastName.setBounds(25, 109, 65, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblLastName);
		
		pwField = new JTextField();
		pwField.setBounds(110, 50, 114, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(pwField);
		pwField.setColumns(10); // $codepro.audit.disable numericLiterals
		
		fNameField = new JTextField();
		fNameField.setBounds(110, 78, 114, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(fNameField);
		fNameField.setColumns(10); // $codepro.audit.disable numericLiterals
		
		lNameField = new JTextField();
		lNameField.setBounds(110, 106, 114, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(lNameField);
		lNameField.setColumns(10); // $codepro.audit.disable numericLiterals
		
		JLabel lblAge = new JLabel("Age:"); // $codepro.audit.disable stringLiterals
		lblAge.setBounds(25, 141, 46, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblAge);
		
		ageField = new JTextField();
		ageField.setBounds(110, 138, 114, 20);
		contentPane.add(ageField);
		ageField.setColumns(10);
		
		JLabel lblGender = new JLabel("Gender:"); // $codepro.audit.disable stringLiterals
		lblGender.setBounds(25, 172, 46, 14);
		contentPane.add(lblGender);
		
		JLabel lblAddress = new JLabel("Address:"); // $codepro.audit.disable stringLiterals
		lblAddress.setBounds(25, 209, 46, 14);
		contentPane.add(lblAddress);
		
		JLabel lblPhone = new JLabel("Phone:"); // $codepro.audit.disable stringLiterals
		lblPhone.setBounds(25, 261, 46, 14);
		contentPane.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:"); // $codepro.audit.disable stringLiterals
		lblEmail.setBounds(25, 292, 46, 14);
		contentPane.add(lblEmail);
		
		phoneField = new JTextField();
		phoneField.setBounds(110, 261, 114, 20);
		contentPane.add(phoneField);
		phoneField.setColumns(10);
		
		emailField = new JTextField();
		emailField.setBounds(110, 292, 114, 20);
		contentPane.add(emailField);
		emailField.setColumns(10);
		
		JButton btnAdd = new JButton("Add"); // $codepro.audit.disable stringLiterals
		btnAdd.setBounds(46, 458, 89, 23);
		contentPane.add(btnAdd);
		
		JButton btnCancel = new JButton("Cancel"); // $codepro.audit.disable stringLiterals
		btnCancel.setBounds(162, 458, 89, 23);
		contentPane.add(btnCancel);
		
		addressArea = new JTextArea();
		addressArea.setBounds(110, 204, 114, 48);
		contentPane.add(addressArea);
		
		rdbtnMale = new JRadioButton("Male"); // $codepro.audit.disable stringLiterals
		rdbtnMale.setSelected(true);
		rdbtnMale.setBounds(112, 168, 52, 23);
		contentPane.add(rdbtnMale);
		
		rdbtnFemale = new JRadioButton("Female"); // $codepro.audit.disable stringLiterals
		rdbtnFemale.setBounds(166, 168, 109, 23);
		contentPane.add(rdbtnFemale);
		
		ButtonGroup genderGroup = new ButtonGroup();
		genderGroup.add(rdbtnMale);
		genderGroup.add(rdbtnFemale);
		
		lblPharmacy = new JLabel("Pharmacy:"); // $codepro.audit.disable stringLiterals
		lblPharmacy.setBounds(25, 324, 65, 14);
		contentPane.add(lblPharmacy);
		
		lblInsurance = new JLabel("Insurance:"); // $codepro.audit.disable stringLiterals
		lblInsurance.setBounds(25, 352, 58, 14);
		contentPane.add(lblInsurance);
		
		lblAllergy = new JLabel("Allergy:"); // $codepro.audit.disable stringLiterals
		lblAllergy.setBounds(25, 392, 46, 14);
		contentPane.add(lblAllergy);
		
		pharmacyField = new JTextField();
		pharmacyField.setBounds(110, 321, 114, 20);
		contentPane.add(pharmacyField);
		pharmacyField.setColumns(10);
		
		insuranceField = new JTextField();
		insuranceField.setBounds(110, 349, 114, 20);
		contentPane.add(insuranceField);
		insuranceField.setColumns(10);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(110, 386, 114, 42);
		contentPane.add(scrollPane);
		
		allergyArea = new JTextArea();
		scrollPane.setViewportView(allergyArea);
		
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				int gender;
				if (rdbtnMale.isSelected())
				{			
					gender = 0;
				}
				else
				{
					gender = 1;
				}
				if (idField.getText().equals("") || pwField.getText().equals("") || fNameField.getText().equals("")
						|| lNameField.getText().equals("") || ageField.getText().equals("") 
						|| addressArea.getText().equals("") || phoneField.getText().equals("") || emailField.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "You must enter all the entries",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
				}
				else
				{
					try
					{
						Patient newPatient = new Patient(idField.getText(), pwField.getText(), fNameField.getText(), lNameField.getText(),
								Integer.parseInt(ageField.getText()), gender, addressArea.getText(), phoneField.getText(),
								emailField.getText());
						
						newPatient.setPharmacy(pharmacyField.getText());
						newPatient.setInsurance(insuranceField.getText());
						newPatient.setAllergies(allergyArea.getText());
						
						if (!presenter.addUser(newPatient))
						{
							JOptionPane.showMessageDialog(null, "Same ID already exists",  // $codepro.audit.disable stringLiterals
									"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
						}
						else
						{
							//Update Model for list
							model.clear();

							for (Patient e : presenter.getPatientList())
							{
								model.addElement(e);
							}
						
							setVisible(false);
							dispose();
						}
					}
					catch (NumberFormatException e)
					{						
						JOptionPane.showMessageDialog(null, "You must enter integers for age",  // $codepro.audit.disable stringLiterals
								"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
					}
				}
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
			}
		});
	}
}
